package com.suyx.job.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.suyx.job.dao.TaskInfoRepository;
import com.suyx.job.dao.TuInfoLogRepository;
import com.suyx.job.entity.DeviceInfo;
import com.suyx.job.entity.Page;
import com.suyx.job.entity.RoleInfo;
import com.suyx.job.entity.TaskInfo;
import com.suyx.job.entity.TuInfoLog;
import com.suyx.job.service.ITaskInfoService;
import com.suyx.job.service.base.impl.MyServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: lanyanhua
 * @Date: 2018/10/18 0018 11:50
 * @Description:
 */
@Slf4j
@Service
public class TaskInfoServiceImpl extends MyServiceImpl<TaskInfo,Integer> implements ITaskInfoService {
	
	private TaskInfoRepository taskInfoRepository;

    @Autowired
    public TaskInfoServiceImpl(TaskInfoRepository repository) {
        super(repository);
        this.taskInfoRepository = repository;
    }

	@Override
	public TaskInfo findByWpTypeAndJqNo(Integer wpType, Integer jqNo) {
		return taskInfoRepository.findByWpTypeAndJqNo(wpType, jqNo);
	}

	@Override
	public TaskInfo findByRoleIdAndTaskText(String roleId, String taskText) {
		return taskInfoRepository.findByRoleIdAndTaskText(roleId, taskText);
	}

	@Override
	public Page findTaskPage(TaskInfo taskInfo, Integer page, Integer limit) {
		return new Page(
				taskInfoRepository.findAll((Root<TaskInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
                    List<Predicate> predicates = new ArrayList<>();
                    if(!StringUtils.isEmpty(taskInfo.getRoleName())){
                        predicates.add(cb.like(root.get("roleName").as(String.class), "%"+taskInfo.getRoleName() + "%"));
                    }
                    if(!StringUtils.isEmpty(taskInfo.getWpName())){
                        predicates.add(cb.like(root.get("wpName").as(String.class), "%"+taskInfo.getWpName() + "%"));
                    }
                    if(!StringUtils.isEmpty(taskInfo.getStatus())){
                        predicates.add(cb.equal(root.get("status").as(String.class), taskInfo.getStatus()));
                    }
                    return  query.where(predicates.toArray(new Predicate[0]))
                    		.orderBy(cb.asc(root.get("status").as(String.class)),cb.desc(root.get("createTime").as(Date.class))).getRestriction();
                }, PageRequest.of(page,limit)));
	}

	@Override
	public TaskInfo findByJqNoAndStatus(Integer jqNo, String status) {
		List<TaskInfo> list = taskInfoRepository.findByJqNoAndStatus(jqNo, status);
		return list.size() > 0 ? list.get(0):null;
	}
  }	
